import {Component, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {UtilityService} from '../../../service/utils.service';
import {ActivatedRoute} from '@angular/router';
import {LogcChangeModule, PageModule} from '../../../service/common.module';
import {appConfig} from '../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import * as moment from 'moment';

@Component({
  selector: 'app-timeline',
  templateUrl: './timeline.component.html',
})
export class TimelineComponent implements OnInit {
    page: any;
    logName: string;
    infoData: any;
    dataInfo: any;
    array = [];
    modalVisible = false;
    loading = false;
    data = [];
    total: number;
    quickMenu = false; // 抽屉按钮默认关闭
    logChange: LogcChangeModule = new LogcChangeModule(); // 对象差异值
    logChangeList: any;
    pages: PageModule = new PageModule();
    constructor(
        private http: _HttpClient,
        public activatedRoute: ActivatedRoute,
        private utilityService: UtilityService,
        private nznot: NzNotificationService
    ) { }
    logGuid: string;
    ngOnInit() {
        this.activatedRoute.queryParams.subscribe(queryParams => {
            this.infoData = JSON.parse(queryParams.logInfo);
            this.logName = this.infoData.dictName;
        });
       /* if(this.infoData.dataString !== '') {
            this.getInfo(JSON.parse(this.infoData.dataString));
        }*/
        this.getData(this.infoData.dataGuid);
        this.getDetial(this.infoData); // 调用日志详情
    }


    // 详情信息--- 还未做，根据后台传给我的数据变化循环改成对应的结构 渲染即可
    getInfo(event) {
        this.dataInfo = event;
        for (let k in this.dataInfo) {
            let  strs = {
                key: k,
                value: this.dataInfo[k]
            };
            this.array.push(strs);
        }
    }

    getData(event) {
        this.page = {
            condition: [
                {
                    "logic": "AND",
                    "queryList": [{
                        "logic": "AND",
                        "operator": "EQ",
                        "params": ['guidData', this.infoData.guid]
                    }
                    ]
                }
            ],
            page: {
                current: this.pages.pi,
                size: this.pages.size,
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.logChangeslist , this.page)
            .subscribe(
                (val) => {

                    for (let i = 0 ; i < val.result.records.length; i++) {
                        val.result.records[i].userName = 'wangbo';
                        val.result.records[i].status = '成功';
                        // val.result.records[i].operateTime = new Date(val.result.records[i].operateTime)
                        val.result.records[i].operateTime = moment(Number(val.result.records[i].operateTime)).format('YYYY-MM-DD HH:MM:ss');
                    }
                    console.log(val.result.records)
                    this.data = val.result.records;
                    this.total = val.result.total;
                }
            );
    }

    // 查询日志操作详情
    getDetial(event) {
        let lastIndex = event.dataClass.lastIndexOf('.') + 1;
        event.className = event.dataClass.slice(lastIndex, event.dataClass.length)
        this.utilityService.getData(appConfig.serverUrl + appConfig.VAPi.entryData + '/' + event.dataGuid + '/getEntity/'  +  event.className)
            .subscribe(
                (val) => {
                    console.log(val.result)
                    this.getInfo(val.result)
                }
            );
    }



    objChange(event) {
        this.quickMenu=!this.quickMenu; // 抽屉是否打开
        this.logChangeList = event.messagesList;
        console.log(this.logChangeList)
    }

    pageChange(event) {

    }

    save() {

    }
}
